<?php


class catModel extends Model{
    protected  $table = 'category';
    protected $pk = 'cat_id';

    // public function __construact(){

    // }
   
//     public function add($data,$mode,$cat_id=''){
//      // echo '执行11111';
//      if(!empty($cat_id)){
//            $sql = ' where cat_id =' . $cat_id;
//           return $this->db->autoExecute($this->table,$data,$mode,$sql);
//      }
//       return  $this->db->autoExecute($this->table,$data,$mode);
//     }

      
      //删除
     public function del($id){
           $sql = 'delete from '.$this->table . ' where cat_id =' . $id ;
           return $this->db->query($sql);
      } 

     // 获取单独某个栏目
     public function getColumnRow($id){
          $sql = 'select * from ' . $this->table . ' where cat_id = ' . $id; 
          return $this->db->getRow($sql);
      } 

     //获取所有的商品栏目
     public function getall(){
      $sql = 'select cat_id,cat_name,parent_id from ' . $this->table;
       return  $cat = $this->db->getAll($sql);
     }


     //递归寻找子孙树，并显示层级 (返回所有的栏目详情)
     public function getlist($id=0,$lev=0){
       $tree = array();
       $data = $this->getall();
        foreach($data as $v){  
            if($v['parent_id'] == $id){
               $v['lev'] = $lev;
                $tree[] = $v;
                $tree = array_merge($tree,$this->getlist($v['cat_id'],$lev+1));
            }
         }
          return $tree;
      }

    /**
     * 递归寻找子孙树 父类栏目下的 所有子孙栏目 （仅返回栏目id）
     * 但是应该是包含父类ld在内的。传入的参数应该是该分类的的父ld
     * @param $parent
     * @return array
     */
       public function lookChildTree($parent){
        $arr = array();
        $data = $this->getall();
        foreach($data as $k=>$v){
            if($v['parent_id'] == $parent){
                    $arr[] = $v['cat_id'];
                    $arr = array_merge($arr,$this->lookChildTree($v['cat_id']));
            }
         }
        return $arr;
       }


    /**
     * 查找某个栏目的家谱树
     * @param $id 栏目的ID
     * @return array
     */
       public function getParent($id){
          static  $parent = array();
          $data = $this->getall();
            foreach($data as $v){
                if($id == $v['cat_id']){
                      $parent[] = $v;
                      $this->getParent($v['parent_id']);
                      // $parent = array_merge($parent,$v);
                }
            }
            return $parent;
       }

}

